//
//  DSPWelcomeController.swift
//  SWweibo
//
//  Created by 杜守鹏 on 16/1/1.
//  Copyright © 2016年 杜守鹏. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage
class DSPWelcomeController: UIViewController {
    //MARK:懒加载头像
    
    lazy var iconImageView:UIImageView = {
     let imageView = UIImageView()
        let image = UIImage(named:"avatar_default_big")
        imageView.layer.cornerRadius = 45
        imageView.layer.masksToBounds = true
       imageView.image = image
        return imageView
    }()
    
    //MARK:懒加载label
    
    lazy var welcomeLabel:UILabel={
      let label = UILabel()
        label.text = "欢迎回来"
        label.alpha = 0
         label.textColor = UIColor.darkGrayColor()
        label.font = UIFont.systemFontOfSize(16)
        return label
    
    }()

    override func viewDidLoad() {
        super.viewDidLoad()
//        print(DSPDSPUserCountViewModel.account()?.uid)
        let urlString = DSPDSPUserCountViewModel.account()?.avatar_large
        let url = NSURL(string: urlString!)
        if  urlString != nil {
            iconImageView.sd_setImageWithURL(url, placeholderImage:  UIImage(named: "avatar_default_big"))
        }
        setUPUI()
    }
//MARK: UI布局
    func setUPUI(){
        
        view.backgroundColor = UIColor(white: 247/255, alpha: 1)
      view.addSubview(welcomeLabel)
        view.addSubview(iconImageView)
        iconImageView.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(view)
            make.top.equalTo(view).offset(200)
            make.size.equalTo(CGSizeMake(90, 90))
        }
        welcomeLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconImageView)
            make.top.equalTo(iconImageView.snp_bottom).offset(10)
        }
    }
    
    override func viewDidAppear(animated: Bool) {
       
        super.viewDidAppear(animated)
        
        iconImageView.snp_updateConstraints { (make) -> Void in
            make.top.equalTo(view).offset(100)
        }
        UIView.animateWithDuration(1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 0, options: [], animations: { () -> Void in
        self.view.layoutIfNeeded()
            }) { (Bool) -> Void in
                
                UIView.animateWithDuration(1, animations: { () -> Void in
                    
                  self.welcomeLabel.alpha = 1
                    
                    }, completion: { (Bool) -> Void in
//                     self.dismissViewControllerAnimated(true , completion: { () -> Void in
//
//                     })
                        let vc = DSPTabBarController()
                        self.presentViewController(vc, animated: true, completion: nil)
                        
                })
        }
    }
    
    deinit{
        print("welcome")
    }
}
